import { gameConfigHandle } from "core/config/gameConfig"
import { MazeModel, TradeItemModel } from "core/model/luckModel"
import { PlayerModel } from "core/model/playerModel"
import Random from "core/utils/random"
import utils from "core/utils/utils"

class LuckHandle {
    resetCard(player: PlayerModel, rand: Random) {
        let card = player.luck.card
        card.cardState = {}
        card.flipTimes = 0
        card.cards = []
        let luckConfig = gameConfigHandle.gameConfig.luck
        //随机新的卡牌
        let toChoseCards = []
        luckConfig.cardsAmount.forEach((amount, i) => {
            for (let j = 0; j < amount; j++) {
                toChoseCards.push(i)
            }
        })
        new Array(25).fill(0).forEach((_, i) => {
            card.cards.push(utils.getOneFromArrayAndRemove(toChoseCards, rand))
        })
        let cardIndexs = luckConfig.cardIndexs.concat()
        card.visibleCards = utils.getRandomArrayNoRepeat(cardIndexs, luckConfig.visibleCardAmount, rand)
    }

    getCardRewardType(player: PlayerModel) {
        let card = player.luck.card
        let type = 0
        let descConfig = gameConfigHandle.gameConfig.luck.rewardDescOfCard
        let desc = ''
        let maxId
        let cardSorces = {}
        card.cards.forEach((id, i) => {
            if (card.cardState[i]) {
                if (cardSorces[id]) {
                    cardSorces[id]++
                } else {
                    cardSorces[id] = 1
                }
            }
        })
        let maxAmount = 0
        Object.entries(cardSorces).forEach((score, i) => {
            if (score[1] as number > maxAmount) {
                maxAmount = score[1] as number
                maxId = score[0]
            }
        })

        let amounts: number[] = Object.values(cardSorces)
        amounts.sort()
        amounts.reverse()
        if (amounts[0] == 5) {
            type = 0
            desc = descConfig[0]
        } else if (amounts[0] == 4) {
            type = 1
            desc = descConfig[1]
        } else if (amounts[0] == 3) {
            if (amounts[1] && amounts[1] >= 2) {
                type = 2
                desc = descConfig[2]
            } else {
                type = 3
                desc = descConfig[3]
            }
        } else if (amounts[0] == 2) {
            if (amounts[1] && amounts[1] == 2) {
                type = 4
                desc = descConfig[4]
            } else {
                type = 5
                desc = descConfig[5]
            }
        } else {
            type = 6
            desc = descConfig[6]
        }
        return { type, desc, maxId }
    }

    getFlipTimesMax(player: PlayerModel, vipLvl: number, isMonthVip: boolean) {

        let times = gameConfigHandle.gameConfig.luck.flipCardTimes
        let vipConfig = gameConfigHandle.gameConfig.vip
        if (isMonthVip) {
            times += vipConfig.monthVip.flipCardTimes
        }
        // times += vipConfig.vipAdd.flipCardTimes[vipLvl]
        return times
    }
    resetMazeMaps(player: PlayerModel, rand: Random) {
        let luckConfig = gameConfigHandle.gameConfig.luck
        let toGetMaps: number[] = []
        player.luck.maze = new MazeModel()
        let maze = player.luck.maze

        //设置起点
        maze.maps.push(3)
        maze.mapState[0] = true

        luckConfig.mazeMapAmounts.forEach((amount, i) => {
            for (let j = 0; j < amount; j++) {
                toGetMaps.push(i)
            }
        })
        for (let i = 0; i < 98; i++) {
            maze.maps.push(utils.getOneFromArrayAndRemove(toGetMaps, rand))
        }

        //避免宝藏离起点太近
        let bzIndex = maze.maps.indexOf(6)
        let randIndex: number = utils.getOneFromArray(luckConfig.bzPoses, rand)
        maze.maps[bzIndex] = maze.maps[randIndex]
        maze.maps[randIndex] = 6
    }
    refreshMazeMaps(player: PlayerModel, rand: Random) {
        let luckConfig = gameConfigHandle.gameConfig.luck
        let toGetMaps: number[] = []
        let maze = player.luck.maze
        let actionPower = maze.actionPower
        let addActionPower = maze.addActionPower
        let buyActionPower = maze.buyActionPower
        player.luck.maze = new MazeModel()
        maze = player.luck.maze
        player.luck.maze.actionPower = actionPower
        player.luck.maze.addActionPower = addActionPower
        player.luck.maze.buyActionPower = buyActionPower
        //设置起点
        maze.maps.push(3)
        maze.mapState[0] = true

        luckConfig.mazeMapAmounts.forEach((amount, i) => {
            for (let j = 0; j < amount; j++) {
                toGetMaps.push(i)
            }
        })
        for (let i = 0; i < 98; i++) {
            maze.maps.push(utils.getOneFromArrayAndRemove(toGetMaps, rand))
        }

        //避免宝藏离起点太近
        let bzIndex = maze.maps.indexOf(6)
        let randIndex: number = utils.getOneFromArray(luckConfig.bzPoses, rand)
        maze.maps[bzIndex] = maze.maps[randIndex]
        maze.maps[randIndex] = 6
    }
    getActionPower(player: PlayerModel, isMonthVip: boolean) {
        let max = gameConfigHandle.gameConfig.luck.actionPowerMax
        if (isMonthVip) {
            max += gameConfigHandle.gameConfig.vip.monthVip.addActionPower
        }
        max += player.luck.maze.addActionPower + player.luck.maze.buyActionPower
        let used = player.luck.maze.actionPower
        let remain = max - used
        return { used, remain, max }
    }
    getExplorePower(player: PlayerModel, vipLvl: number, isMonthVip: boolean) {
        let max = gameConfigHandle.gameConfig.luck.explorePowerMax
        if (isMonthVip) {
            max += gameConfigHandle.gameConfig.vip.monthVip.addExplorePower
        }
        max += player.luck.front.buyTimes
        let used = player.luck.front.times
        let remain = max - used
        let canBuy = gameConfigHandle.gameConfig.vip.vipAdd.buyExplorePowerMax[vipLvl] - player.luck.front.buyTimes
        return { used, remain, max, canBuy }
    }
    //寻宝地图是否可以达到
    canReachMazeMap(player: PlayerModel, index: number) {
        let can = false
        let mapState = player.luck.maze.mapState
        let maps = player.luck.maze.maps
        //行: row;列: column
        let row = Math.floor(index / 9)
        let column = index % 9
        if (row == 0) {
            if (column == 0) {
                can = true
            } else if (column == 8) {
                if ((mapState[index - 1] && maps[index - 1] == 3) || (mapState[index + 9] && maps[index + 9] == 3)) {
                    can = true
                }
            } else {
                if ((mapState[index - 1] && maps[index - 1] == 3) || (mapState[index + 9] && maps[index + 9] == 3) ||
                    (mapState[index + 1] && maps[index + 1] == 3)) {
                    can = true
                }
            }
        } else if (row == 11) {
            if (column == 0) {
                if ((mapState[index + 1] && maps[index + 1] == 3) || (mapState[index - 9] && maps[index - 9] == 3)) {
                    can = true
                }
            } else if (column == 8) {
                if ((mapState[index - 1] && maps[index - 1] == 3) || (mapState[index - 9] && maps[index - 9] == 3)) {
                    can = true
                }
            } else {
                if ((mapState[index - 1] && maps[index - 1] == 3) || (mapState[index - 9] && maps[index - 9] == 3) ||
                    (mapState[index + 1] && maps[index + 1] == 3)) {
                    can = true
                }
            }
        } else {
            if (column == 0) {
                if ((mapState[index + 9] && maps[index + 9] == 3) || (mapState[index - 9] && maps[index - 9] == 3) ||
                    (mapState[index + 1] && maps[index + 1] == 3)) {
                    can = true
                }
            } else if (column == 8) {
                if ((mapState[index + 9] && maps[index + 9] == 3) || (mapState[index - 9] && maps[index - 9] == 3) ||
                    (mapState[index - 1] && maps[index - 1] == 3)) {
                    can = true
                }
            } else {
                if ((mapState[index + 9] && maps[index + 9] == 3) || (mapState[index - 9] && maps[index - 9] == 3) ||
                    (mapState[index + 1] && maps[index + 1] == 3) || (mapState[index - 1] && maps[index - 1] == 3)) {
                    can = true
                }
            }
        }
        return can
    }
    //
    getWortshipTimes(player: PlayerModel, vipLvl: number, isMonthVip: boolean) {
        let vipConfig = gameConfigHandle.gameConfig.vip
        let luckConfig = gameConfigHandle.gameConfig.luck
        let worship = player.luck.worship
        let maxTimes = luckConfig.worshipTimes + worship.buyTimes + (isMonthVip ? vipConfig.monthVip.worshipTimes : 0)
        let haveTimes = maxTimes - worship.times
        let canBuyTimes = vipConfig.vipAdd.buyWorshipTimes[vipLvl] - worship.buyTimes
        let freeTimes = luckConfig.worshipTimes + (isMonthVip ? vipConfig.monthVip.worshipTimes : 0)
        return { maxTimes, haveTimes, canBuyTimes, freeTimes }
    }
    getTradeTimes(player: PlayerModel, vipLvl: number, isMonthVip: boolean) {
        let vipConfig = gameConfigHandle.gameConfig.vip
        let luckConfig = gameConfigHandle.gameConfig.luck
        let trade = player.luck.trade
        let maxTimes = luckConfig.tradeTimes + trade.buyTimes + (isMonthVip ? vipConfig.monthVip.tradeTimes : 0)
        let haveTimes = maxTimes - trade.times
        let canBuyTimes = vipConfig.vipAdd.buyTradeTimes[vipLvl] - trade.buyTimes
        let freeTimes = luckConfig.tradeTimes + (isMonthVip ? vipConfig.monthVip.tradeTimes : 0)
        return { maxTimes, haveTimes, canBuyTimes, freeTimes }
    }
    resetTradeGoods(player: PlayerModel, rand: Random) {
        player.luck.trade.tradeItems = []
        let luckConfig = gameConfigHandle.gameConfig.luck
        let toChoseUseItems = luckConfig.tradeUseIndexs.concat()
        let toChoseGetItems = luckConfig.tradeGetIndexs.concat()
        new Array(luckConfig.tradeGoodsAmountMax).fill(0).forEach((_, i) => {
            let tradeItem = new TradeItemModel()
            tradeItem.useItem = utils.getOneFromArrayAndRemove(toChoseUseItems, rand)
            tradeItem.getItem = utils.getOneFromArrayAndRemove(toChoseGetItems, rand)
            player.luck.trade.tradeItems.push(tradeItem)
        })
    }
    resetFrontItems(player: PlayerModel, rand: Random) {
        let luckConfig = gameConfigHandle.gameConfig.luck
        let toGetMaps: number[] = []
        let front = player.luck.front
        front.items = []
        front.openIndexs = []

        luckConfig.frontItemAmounts.forEach((amount, i) => {
            for (let j = 0; j < amount; j++) {
                toGetMaps.push(i)
            }
        })
        for (let i = 0; i < 9; i++) {
            front.items.push(utils.getOneFromArrayAndRemove(toGetMaps, rand))
        }
    }
    //判断是否可以去下一层
    isFrontExploreComplete(player: PlayerModel) {
        let front = player.luck.front
        let isOk = true
        front.items.forEach((item, i) => {
            if (item == 3 && !front.openIndexs.includes(i)) {
                isOk = false
            }
            if ((item == 0 || item == 1 || item == 2) && front.openIndexs.includes(i)) {
                isOk = false
            }
        })
        if (front.items.length !== 9) {
            isOk = false
        }
        return isOk
    }
}

let luckHandle = new LuckHandle()

export default luckHandle